package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.api.domain.SignIn;
import com.juyu.miliao.api.util.RedisKeys;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName: SignInRedisDao
 * @Description: 签到有理
 * @Author zhangcheng
 * @Date 19-9-11下午1:55
 */
@Repository
public class SignInRedisDao {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取用户签到数据
     * @param userId
     * @return
     */
    public Optional<SignIn> getSignInData(Long userId) {
        List<Object> valueList = stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId))
                .multiGet(Arrays.asList("consecutiveDays", "vipConsecutiveDays", "signed",
                        "vipReward0", "vipReward1", "vipReward2", "vipReward3", "vipReward4"));
        if(valueList.get(0) == null) {
            return Optional.empty();
        } else {
            SignIn signIn = new SignIn();
            signIn.setConsecutiveDays(Integer.parseInt((String)valueList.get(0)));
            signIn.setVipConsecutiveDays(Integer.parseInt((String)valueList.get(1)));
            signIn.setSigned(Boolean.parseBoolean((String) valueList.get(2)));
            List<Integer> vipRewardList = new ArrayList<>(5);
            vipRewardList.add(Integer.parseInt((String)valueList.get(3)));
            vipRewardList.add(Integer.parseInt((String)valueList.get(4)));
            vipRewardList.add(Integer.parseInt((String)valueList.get(5)));
            vipRewardList.add(Integer.parseInt((String)valueList.get(6)));
            vipRewardList.add(Integer.parseInt((String)valueList.get(7)));
            signIn.setVipRewardList(vipRewardList);
            return Optional.of(signIn);
        }
    }

    /**
     * 初始化用户签到次数
     * @param userId
     */
    public void initSignInData(Long userId) {
        Map<String, Object> signInData = new HashMap<>(16);
        signInData.put("consecutiveDays", "0");
        signInData.put("vipConsecutiveDays", "0");
        signInData.put("signed", "false");
        signInData.put("vipReward0", "0");
        signInData.put("vipReward1", "0");
        signInData.put("vipReward2", "0");
        signInData.put("vipReward3", "0");
        signInData.put("vipReward4", "0");
        stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId)).putAll(signInData);
    }

    /**
     * 更新用户签到天数
     * @param userId
     */
    public void updateConsecutiveDays(Long userId) {
        BoundHashOperations boundHashOperations = stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId));
        boundHashOperations.increment("consecutiveDays", 1);
        boundHashOperations.put("signed", "true");
    }

    /**
     * 更新vip累计签到天数
     * @param userId
     */
    public void updateVipConsecutiveDays(Long userId) {
        BoundHashOperations boundHashOperations = stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId));
        boundHashOperations.increment("vipConsecutiveDays", 1);
    }

    /**
     * 更新vip奖励状态
     * @param userId 用户id
     */
    public void updateVipReward(Long userId, Long vipRewardId) {
        stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId))
                .increment("vipReward" + vipRewardId, 1);
    }

    /**
     * 重置vip累计签到天数
     * @param userId
     */
    public void resetVipConsecutiveDays(Long userId) {
        Map<String, Object> signInData = new HashMap<>(16);
        signInData.put("vipConsecutiveDays", "0");
        signInData.put("vipReward0", "0");
        signInData.put("vipReward1", "0");
        signInData.put("vipReward2", "0");
        signInData.put("vipReward3", "0");
        signInData.put("vipReward4", "0");
        stringRedisTemplate.boundHashOps(RedisKeys.USER_SIGN_IN.getValue(userId))
                .putAll(signInData);

    }

}
